package cn.edu.ctbu.web.restcontroller;

import cn.edu.ctbu.web.dao.AdminCourse;
import cn.edu.ctbu.web.dao.AdminGrade;
import cn.edu.ctbu.web.domain.Student;
import cn.edu.ctbu.web.domain.Teacher;
import cn.edu.ctbu.web.helper.PageUtils;
import cn.edu.ctbu.web.helper.R;
import cn.edu.ctbu.web.service.AdminService;
import cn.edu.ctbu.web.service.StudentService;
import cn.edu.ctbu.web.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/webapi/admin")
public class AdminRestController {

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    AdminService adminService;

    /**
     * 查询所有学生的信息
     * @param pageNum 当前页码
     * @param pageSize 每一页的记录数
     * @return 所有学生的信息
     */
    @GetMapping(value = "/getallstudent")
    public PageUtils getAllStudent(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3") int pageSize){
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Student> studentPage = studentService.findAllStudent(pageable);
        PageUtils pageUtils = new PageUtils(studentPage.getTotalElements(), studentPage.getContent());
        return pageUtils;
    }

    /**
     * 获取所有教师的信息
     * @param pageNum 当前页码
     * @param pageSize 每一页的记录数
     * @return 分页结果
     */
    @GetMapping(value = "/getallteacher")
    public PageUtils getAllTeacher(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Teacher> teacherPage = teacherService.findAllTeacher(pageable);
        PageUtils pageUtils = new PageUtils(teacherPage.getTotalElements(), teacherPage.getContent());
        return pageUtils;
    }

    /**
     * 获取所有课程的信息
     * @param pageNum 当前页码
     * @param pageSize 每一页的记录数
     * @return 所有课程的信息
     */
    @GetMapping(value = "/getallcourse")
    public PageUtils getAllCourse(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3") int pageSize){
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<AdminCourse> coursePage = adminService.findAllCourse(pageable);
        return PageUtils.builder()
                .total(coursePage.getTotalElements())
                .rows(coursePage.getContent())
                .build();
    }

    /**
     * 获取所有成绩的信息
     * @param pageNum 当前页数
     * @param pageSize 每一页的记录数
     * @return 所有成绩的信息
     */
    @GetMapping(value = "/getallgrade")
    public PageUtils getAllGrade(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3") int pageSize){
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<AdminGrade> gradePage = adminService.findAllGrade(pageable);
        return PageUtils.builder()
                .total(gradePage.getTotalElements())
                .rows(gradePage.getContent())
                .build();
    }

    /**
     * 新增课程表信息
     * @param cno 课程编号
     * @param cname 课程名称
     * @param tno 教师编号
     * @param credit 课程学分
     */
    @PostMapping(value = "/insertCourse/{cno}&{cname}&{tno}&{credit}")
    public R insertCourseAdmin(@PathVariable("cno") String cno, @PathVariable("cname") String cname,
                               @PathVariable("tno") String tno, @PathVariable("credit") int credit) throws Exception{
        adminService.insertCourseAdmin(cno,cname,tno,credit);
        return R.ok();
    }

    /**
     * 删除课程表信息
     * @param id 课程id
     */
    @DeleteMapping(value = "/deleteCourse/{id}")
    public R deleteCourseAdmin(@PathVariable("id") String id) throws Exception{
        adminService.deleteCourseAdmin(id);
        return R.ok();
    }


    /**
     * 更新课程表
     * @param cno 课程编号
     * @param cname 课程名称
     * @param tno 教师编号
     * @param credit 课程学分
     * @param id 课程id
     */
    @PostMapping(value = "/updateCourse/{cno}&{cname}&{tno}&{credit}&{id}")
    public R updateCourseAdmin(@PathVariable("cno") String cno, @PathVariable("cname") String cname, @PathVariable("tno") String tno,
                                  @PathVariable("credit") int credit, @PathVariable("id") int id) throws Exception{
        adminService.updateCourseAdmin(cno, cname, tno, credit, id);
        return R.ok();
    }

    /**
     * 查询课程表信息
     * @param cname 课程名称
     * @param pageNum 当前页码
     * @param pageSize 每一页的记录数
     * @return 分页结果
     */
    @GetMapping(value = "/searchCourse/{cname}")
    public PageUtils searchCourseAdmin(@PathVariable("cname") String cname, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3") int pageSize) {

        // 创建 Pageable 对象
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        // 调用服务层方法获取分页数据
        Page<AdminCourse> coursePage = adminService.searchCourseAdmin(cname, pageable);

        // 使用 PageUtils 封装结果
        return PageUtils.builder()
                .total(coursePage.getTotalElements())
                .rows(coursePage.getContent())
                .build();
    }
}
